package com.tz.web;

import com.tz.auth.AuthPassport;
import com.tz.bean.AdminUser;
import com.tz.service.adminUser.IAdminUserService;
import com.tz.util.TmStringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import static com.tz.util.TzConstant.*;


/**
 * 安全管理
 */
@Controller
@RequestMapping("/admin/safe")
public class SafeController {

    @Resource
    private IAdminUserService adminUserService;


    /**
     * 核对原始密码功能
     */
    @ResponseBody
    @RequestMapping("/getPwd")
    public String getPwd(Integer id , String pass){
        //去后台查询
        AdminUser user = adminUserService.get(id);
        String getPass = user.getPassword();
        String nowPass = TmStringUtils.md5Base64(pass);
        if(getPass.equals(nowPass)){
            return "success";
        }
        return "fail";
    }


    /**
     * 修改密码
     * @param adminuser
     * @return
     */

    @AuthPassport
    @ResponseBody
    @RequestMapping("/changePwd")
    public String changePwd(AdminUser adminuser){
        adminuser.setPassword(TmStringUtils.md5Base64(adminuser.getPassword()));
        adminUserService.update(adminuser);
        System.out.println(adminuser.getPassword()+"========");
        return "success";
    }


    /**
     * 进入修改密码页面
     * @param session
     * @param model
     * @return
     */
    @AuthPassport
    @RequestMapping("/updatePwd")
    public String updatePwd(HttpSession session , Model model){
        AdminUser adminUser = (AdminUser) session.getAttribute(SESSION_USER);
        model.addAttribute("adminUser" , adminUser);
        return "safe/updatePwd";
    }

    /**
     * 进入 给其他管理员重置密码 页面
     * @param session
     * @param model
     * @return
     */
    @AuthPassport
    @RequestMapping("/resetPwd")
    public String resetPwd(HttpSession session , Model model){
        AdminUser adminUser = (AdminUser) session.getAttribute(SESSION_USER);
        model.addAttribute("adminUser" , adminUser);
        return "safe/resetPwd";
    }


}
